package com.jxmcloud.business.cut.service.impl.database;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jxmcloud.business.cut.dto.client.UserActivityInfoDto;
import com.jxmcloud.business.cut.mapper.database.IUserActivityInfoDBMapper;
import com.jxmcloud.business.cut.model.database.DBUserActivityInfo;
import com.jxmcloud.business.cut.model.query.UserActivityInfoQuery;
import com.jxmcloud.business.cut.service.database.IUserActivityInfoDBService;
import com.jxmcloud.business.cut.util.MyTool;
import com.jxmcloud.business.cut.vo.UserActivityInfoVo;
import com.xlcloud.platform.util.PublicUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserActivityInfoDBServiceImpl extends ServiceImpl<IUserActivityInfoDBMapper, DBUserActivityInfo> implements IUserActivityInfoDBService {
    @Override
    public boolean insertIntoData(DBUserActivityInfo info) {
        return this.baseMapper.insert(info) == 1;
    }

    @Override
    public DBUserActivityInfo selectOne(Integer uid, Integer activityId, Integer status) {
        QueryWrapper<DBUserActivityInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        queryWrapper.eq("activity_id", activityId);
        queryWrapper.eq("status", status);
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<DBUserActivityInfo> selectAll(Integer uid, Integer activityId) {
        QueryWrapper<DBUserActivityInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        queryWrapper.eq("activity_id", activityId);
        return this.baseMapper.selectList(queryWrapper);
    }


    @Override
    public boolean deleteData(Integer uid) {
        return this.baseMapper.deleteById(uid) == 1;
    }

    @Override
    public boolean updateData(DBUserActivityInfo info) {
        return this.baseMapper.updateById(info) == 1;
    }

    @Override
    public boolean updateNewsCount(Integer uid, Integer activityId, Integer newsCount) {
        DBUserActivityInfo dbUserActivityInfo = this.baseMapper.selectByUidAndActivityIdOnDoing(uid);
        if (dbUserActivityInfo != null) {
            dbUserActivityInfo.setNewUserCount(newsCount);
            return this.baseMapper.updateById(dbUserActivityInfo) == 1;
        }
        return false;
    }

    @Override
    public boolean updateShareCount(Integer uid, Integer activityId, Integer shareCount) {
        DBUserActivityInfo dbUserActivityInfo = this.baseMapper.selectByUidAndActivityIdOnDoing(uid);
        if (dbUserActivityInfo != null) {
            dbUserActivityInfo.setShareCount(shareCount);
            return this.baseMapper.updateById(dbUserActivityInfo) == 1;
        }
        return false;
    }

    @Override
    public boolean updateBeganTime(Integer uid, Integer activityId, Integer beganTime) {
        DBUserActivityInfo dbUserActivityInfo = this.baseMapper.selectByUidAndActivityIdOnDoing(uid);
        if (dbUserActivityInfo != null) {
            Date beganTimeDate = MyTool.toDateByLong(Long.valueOf(beganTime));
            dbUserActivityInfo.setBeganTime(beganTimeDate);
            return this.baseMapper.updateById(dbUserActivityInfo) == 1;
        }
        return false;
    }

    @Override
    public boolean updateNoticeList(Integer uid, Integer activityId, Integer[] noticeList) {
        DBUserActivityInfo dbUserActivityInfo = this.selectByUidAndActivityIdOnDoing(uid);
        if (dbUserActivityInfo != null) {
            dbUserActivityInfo.setNotice(noticeList);
            return this.baseMapper.updateById(dbUserActivityInfo) == 1;
        }
        return false;
    }

    @Override
    public DBUserActivityInfo selectByUidAndActivityIdOnDoing(Integer uid) {
        return this.baseMapper.selectByUidAndActivityIdOnDoing(uid);
    }

    @Override
    public IPage<UserActivityInfoVo> getCutActivityPage(UserActivityInfoQuery query) {
        return this.baseMapper.selectUserActivityInfoPage(query);
    }

    @Override
    public void updateUserActivityInfoForWeb(UserActivityInfoDto dto) {
        DBUserActivityInfo dbUserActivityInfo = this.getById(dto.getId());
        if (dbUserActivityInfo != null) {
            BeanUtils.copyProperties(dto, dbUserActivityInfo);
            //if (dto.getScorePool() != null) {
                //dbUserActivityInfo.setScorePool(dto.getScorePool());
            //}
            this.baseMapper.updateById(dbUserActivityInfo);
        }
    }

    @Override
    public void deleteUserActivityInfo(Integer id) {
        this.baseMapper.deleteById(id);
    }

    @Override
    public DBUserActivityInfo selectByActivityStartId(Integer uid, Integer activityId,Integer activityStartId) {
        QueryWrapper<DBUserActivityInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("uid", uid);
        queryWrapper.eq("activity_id", activityId);
        queryWrapper.eq("activity_start_id", activityStartId);
        return this.baseMapper.selectOne(queryWrapper);
    }

    @Override
    public List<DBUserActivityInfo> selectAllByUid(Integer uid) {
        PublicUtil.ifNullAndThrow(uid,"入参不能为空");
        return this.lambdaQuery()
                .eq(DBUserActivityInfo::getUid,uid)
                .orderByDesc(DBUserActivityInfo::getCreateTime)
                .list();
    }
}
